package com.example.apt;

import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

/**
 * Created by Administrator on 2018/3/22 0022.
 */
@AutoService(Processor.class)
@SupportedAnnotationTypes({"com.example.myproject.annotations.Route"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class TestProcessor extends AbstractProcessor {

    private Messager messager;
    Filer filer;


    public synchronized void init(ProcessingEnvironment processingEnvironment){
        super.init(processingEnvironment);
        filer=processingEnvironment.getFiler();
        messager=processingEnvironment.getMessager();
        messager.printMessage(Diagnostic.Kind.NOTE,"TestProcessor=====init");
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {

        messager.printMessage(Diagnostic.Kind.NOTE,"TestProcessor=====Process");
        for (TypeElement  typeElement:set){
            MethodSpec main=MethodSpec.methodBuilder("main")
                    .addModifiers(Modifier.PUBLIC,Modifier.STATIC)
                    .returns(void.class)
                    .addParameter(String[].class,"args")
                    .addStatement("$T.out.println($S)",System.class,"Hello , JavaPoet")
                    .build();

            TypeSpec helloWorld=TypeSpec.classBuilder("HelloWorld")
                    .addModifiers(Modifier.PUBLIC,Modifier.FINAL)
                    .addMethod(main)
                    .build();

            JavaFile javaFile=JavaFile.builder(
                    "com.example.helloworld",helloWorld
            ).build();

            try {
                javaFile.writeTo(filer);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
}
